As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
A class is a user-defined data type. It consists of data members and member functions, which can be accessed and used by creating an instance of that class. It represents the set of properties or methods that are common to all objects of one type. A class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the class, and wheels, speed limits, mileage are their properties.
It is a basic unit of Object-Oriented Programming and represents the real-life entities. An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated. An object has an identity, state, and behavior. Each object contains data and code to manipulate the data. Objects can interact without having to know details of each others data or code, it is sufficient to know the type of message accepted and type of response returned by the objects.
For example “Dog” is a real-life Object, which has some characteristics like color, Breed, Bark, Sleep, and Eats.
Data abstraction is one of the most essential and important features of object-oriented programming. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation. Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of the car or applying brakes will stop the car, but he does not know about how on pressing the accelerator the speed is increasing, he does not know about the inner mechanism of the car or the implementation of the accelerator, brakes, etc in the car. This is what abstraction is.
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. In Encapsulation, the variables or data of a class are hidden from any other class and can be accessed only through any member function of their class in which they are declared. As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section, etc. The finance section handles all the financial transactions and keeps records of all the data related to finance. Similarly, the sales section handles all the sales-related activities and keeps records of all the sales. Now there may arise a situation when for some reason an official from the finance section needs all the data about sales in a particular month. In this case, he is not allowed to directly access the data of the sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data. This is what encapsulation is. Here the data of the sales section and the employees that can manipulate them are wrapped under a single name “sales section”.
Inheritance is an important pillar of OOP(Object-Oriented Programming). The capability of a class to derive properties and characteristics from another class is called Inheritance. When we write a class, we inherit properties from other classes. So when we create a class, we do not need to write all the properties and functions again and again, as these can be inherited from another class that possesses it. Inheritance allows the user to reuse the code whenever possible and reduce its redundancy.
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. For example, A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism.
In dynamic binding, the code to be executed in response to the function call is decided at runtime. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run time. Dynamic Method Binding One of the main advantages of inheritance is that some derived class D has all the members of its base class B. Once D is not hiding any of the public members of B, then an object of D can represent B in any context where a B could be used. This feature is known as subtype polymorphism.
In the above program, the class Encapsulate is encapsulated as the variables are declared as private. The get methods like getAge() , getName() , getRoll() are set as public, these methods are used to access these variables. The setter methods like setName(), setAge(), setRoll() are also declared as public and are used to set the values of the variables.
The extends keyword is used for inheritance in java. Using the extends keyword indicates you are derived from an existing class. In other words, “extends” refers to increased functionality.
class derived-class extends base-class
{
//methods and fields
}
Example: In the below example of inheritance, class Bicycle is a base class, class MountainBike is a derived class that extends the Bicycle class and class Test is a driver class to run the program.
In the above program, when an object of MountainBike class is created, a copy of all methods and fields of the superclass acquires memory in this object. That is why by using the object of the subclass we can also access the members of a superclass.
Example 2: In the below example of inheritance, class Employee is a base class, class Engineer is a derived class that extends the Employee class and class Test is a driver class to run the program.
Types of Inheritance in Java
Below are the different types of inheritance which are supported by Java.
In single inheritance, subclasses inherit the features of one superclass. In the image below, class A serves as a base class for the derived class B.
In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes. In the below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members.
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the below image, class A serves as a base class for the derived classes B, C, and D.
In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritances with classes. In java, we can achieve multiple inheritances only through Interfaces. In the image below, Class C is derived from interfaces A and B.
It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.
In sub-classes we can inherit members as is, replace them, hide them, or supplement them with new members:
A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person possesses different behavior in different situations. This is called polymorphism.
Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.
In Java polymorphism is mainly divided into two types:
It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading.
Note: But Java doesn’t support the Operator Overloading.
It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding. Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
Subtype of Run-time Polymorphism:
Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.
Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of a car or applying brakes will stop the car, but he does not know how on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of the accelerator, brakes, etc in the car. This is what abstraction is.
In java, abstraction is achieved by interfaces and abstract classes. We can achieve 100% abstraction using interfaces.
There are situations in which we will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. Sometimes we will want to create a superclass that only defines a generalization form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.
Consider a classic “shape” example, perhaps used in a computer-aided design system or game simulation. The base type is “shape” and each shape has a color, size, and so on. From this, specific types of shapes are derived(inherited)-circle, square, triangle, and so on — each of which may have additional characteristics and behaviors. For example, certain shapes can be flipped. Some behaviors may be different, such as when you want to calculate the area of a shape. The type hierarchy embodies both the similarities and differences between the shapes.
The core concepts of OOP are Abstraction, Encapsulation, Inheritance, and Polymorphism. Abstraction is the process of concealing complex logic by defining the code in a separate private method by hiding its implementation. Whatever functionality needed to use can be called by using its method identifier to get the result.
The concept of encapsulation is nothing but a Class used to hide the fields that hold data and the methods to perform the functions that can be accessed based on the access modifier. Polymorphism is the process of defining the same method multiple times to utilize based on data types or arguments. There are two types of polymorphism which are called runtime polymorphism and compile-time polymorphism.
A Class in Object-oriented Programming is ideally called a template or a blueprint of an object. An object of its class type will have the same properties as defined in the implementation of Class. An object will always be a specific instance of a class. A Class can have different subclasses and superclasses. A class can be a child class, or a parent class depends on its declaration. A subclass can have all the properties that its superclass has, whereas the superclass cannot have the subclass’s properties. Another class can extend a class, and it will be called a parent class.
A constructor is a method used to invoke the object creation process by initializing the object’s state. The name of the constructor should be the class’s name ideally and may vary depending on the type of programming language. A constructor must not have any return type.
This is the most common OOP Interview Questions which is asked in an interview. A Destructor is a method that is invoked when the object is destroyed or when its’ scope is about to end. The method will be called explicitly or implicitly based on the programming language used. In C++, the destructor needs to be called, whereas in Java, it is not needed, and a garbage collection mechanism will handle it.
An abstract class is used to define at least one abstract method, but an object cannot be created from it. Classes created using abstract classes are called derived classes. An abstract class will not contain implementation code in its base class. If an abstract class is forced to instantiate an object out of it, a compilation error will be thrown.
Multiple inheritances are the process of extending the behaviors of multiple classes by a single class. It creates an ambiguous situation when runtime to decide which class’ behavior has to be executed. C++ supports multiple inheritances, whereas Java doesn’t support them. The ambiguity and complex issues create a diamond problem defined as extending which parent class upon extending multiple classes.
Static binding is also called early binding, which happens at compile-time, whereas Dynamic binding is called late binding, which happens at the time of run time. An example of static binding is method overloading, and dynamic binding is method overriding. The process of binding for static, final and private methods will always be done at compile-time, whereas the overriding is done at runtime. The process of binding overloaded methods is called static, whereas overridden methods are called dynamic.
Operator overloading is sometimes called ad-hoc polymorphism, which is defined as different operators will have different mechanisms based on the placement of operators and the arguments. In operator overloading, the semantics of the programming language is redefined, which is not usually recommended. In the case of the Java programming language, it was decided by the creators not to use this feature as Java is a production language mostly.
These are the frequently asked OOP Interview Questions which is asked in an interview. Exception handling is the feature available in most object-oriented programming languages, which are defined as the process of handling exceptions during the execution of program flow. The flow of execution should be altered based on the outcome of the exception of aroused. The general blocks of exception handling include try, catch and throw for most of the programming languages like C++, Java etc. In the try block, the code that needs to be executed will be placed, and the catch block will handle the exception, and the thrown block will return the type of exception and error if it can’t be handled. This is the safest way of handling applications to safeguard the flow of the working application.
The OOPS concepts’ main benefits are modularity, extensibility, simplicity, reusability, maintainability, modifiability, etc. The complexity of the programming can be reduced, and the coding structure can be made clear. The different complex functionalities can be decoupled using different classes and implementation methods around the application. The reusability feature provides minor changes in the code whenever needed, which provides the adaptability for the code changes or functionality changes.
The objects of the different classes can be reused in different implementation classes to use their features completely. The maintenance process becomes easier if the code is maintained in an organised way.
Accusam nonumy clita sed rebum kasd eirmod elitr. Ipsum ea lorem at et diam est, tempor rebum ipsum sit ea tempor stet et consetetur dolores. Justo stet diam ipsum lorem vero clita diam
Copyright © PlacePrep. All Rights Reserved.